#!/usr/bin/env bash

query_string="image"

start_time=$(date +'%R')
total=$(find . -type f -name '*.24x24.svg' | wc | awk '{print $1}')
current=0
for f in $(find . -type f -name '*.24x24.svg')
do
	current=$((current+1))
	file=$(realpath ${f})
	layer_id=$(inkscape -z ${file} --query-all  | grep --color=none "${query_string}" | awk -F, '{print $1}')
	if test -n "${layer_id}"
	then
		echo -ne "${current}/${total} ${start_time}>$(date +'%R') inkscape -g ${file} --select=${layer_id} --verb=LayerDelete --verb=FileSave --verb=FileClose --verb=FileQuit ..." &&\
		inkscape ${file} --select=${layer_id} --verb=LayerDelete --verb=FileSave  --verb=FileClose --verb=FileQuit 1>/dev/null 2>/dev/null &&\
		echo "DONE"
	else
		echo "File: ${file} does not have image layer, nothing to do ... DONE"
	fi
done

exit 0
